<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="java.io.*,com.jspsmart.upload.SmartUpload"%>
<%@ page import="com.running.util.DateUtils"%>
<%@ page import="com.running.crm.CrmConstants"%>
<%@ page import="com.running.util.StringUtils"%>
<%@ page import="com.running.util.ImageZipUtils"%>
<%
	SmartUpload su = new SmartUpload();
	//上传初始化
	su.initialize(pageContext);
	// 设定上传限制
	//1.限制每个上传文件的最大长度//10M
	su.setMaxFileSize(10485760);
	//2.限制总上传数据的长度。
	su.setTotalMaxFileSize(10485760*10);
	//3.设定允许上传的文件（通过扩展名限制）,仅允许doc,txt文件。
	su.setAllowedFilesList("jpg,JPG,jpeg,JPEG,gif,GIF");
	boolean sign = true;
	String fileName = "";
	String rootDir = config.getServletContext().getRealPath("/").replaceAll("\\\\","/");	//根目录
	if (rootDir.endsWith("/")) {
		rootDir = rootDir.substring(0,rootDir.length()-1);
	}

	//相对目径
	String fileDir = CrmConstants.MMS_BASE_DIR ;
	
	//物理绝对路径=root+"/"+fileDir
	String sUploadDir = rootDir + "/" +fileDir;

	int fileSize = 0;
	//4.设定禁止上传的文件（通过扩展名限制）,禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
	try {
		//在物理绝对路径上创建文件夹
		new File(sUploadDir).mkdirs();
		su.setDeniedFilesList("exe,bat,jsp,htm,html");
		//上传文件
		su.upload();
		for (int i=0;i<su.getFiles().getCount();i++){
            com.jspsmart.upload.File myFile = su.getFiles().getFile(i);
            if (!myFile.isMissing()) {
              fileName = DateUtils.getDate("yyMMddHHmmssSSSS")+"."+myFile.getFileExt();
              //上传后的HTTP文件路径
              myFile.saveAs(sUploadDir+"/"+fileName);
              if ("zip".equals(StringUtils.varFormat(su.getRequest().getParameter("zip")))) {
              	ImageZipUtils.zipImageFile(sUploadDir+"/"+fileName,200,150,0.5f,"");
              }
              //File file = new File(sUploadDir+"/"+fileName);
              //fileSize = (int)(file.length()/1024)+1;
              fileSize = (myFile.getSize()/1024) + 1;
            }
         }
	} catch (Exception e) {
		e.printStackTrace();
		sign = false;
	}
	if(sign==true){
		out.println("<script>parent.callback('"+fileDir+"','"+fileName+"','"+fileSize+"','upload file success')</script>");
	}else{
		out.println("<script>parent.callback('','','0','upload file error')</script>");
	}
%>